﻿//-----------------------------------------------------------------------
// <copyright file="ColorRGBA.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace TileTool.Common.Math
{
    using System;

    /// <summary>
    /// RGBA color structure.
    /// </summary>
    public struct ColorRGBA
    {
        #region Fields - Public

        /// <summary>
        /// Red component.
        /// </summary>
        public byte r;

        /// <summary>
        /// Green component.
        /// </summary>
        public byte g;

        /// <summary>
        /// Blue component.
        /// </summary>
        public byte b;

        /// <summary>
        /// Alpha component.
        /// </summary>
        public byte a;

        #endregion

        #region Constructor

        /// <summary>
        /// Construct the object with the passed RGBA color.
        /// </summary>
        /// <param name="r">Red component</param>
        /// <param name="g">Green component</param>
        /// <param name="b">Blue component</param>
        /// <param name="a">Alpha component</param>
        public ColorRGBA(byte r, byte g, byte b, byte a)
        {
            this.r = r;
            this.g = g;
            this.b = b;
            this.a = a;
        }

        #endregion

        #region Properties - Public static

        /// <summary>
        /// Gets the color white.
        /// </summary>
        public static ColorRGBA White
        {
            get
            {
                return new ColorRGBA(255, 255, 255, 255);
            }
        }

        /// <summary>
        /// Gets the color black.
        /// </summary>
        public static ColorRGBA Black
        {
            get
            {
                return new ColorRGBA(0, 0, 0, 255);
            }
        }

        /// <summary>
        /// Gets the color gray.
        /// </summary>
        public static ColorRGBA Gray
        {
            get
            {
                return new ColorRGBA(128, 128, 128, 255);
            }
        }

        /// <summary>
        /// Gets the color red.
        /// </summary>
        public static ColorRGBA Red
        {
            get
            {
                return new ColorRGBA(255, 0, 0, 255);
            }
        }

        /// <summary>
        /// Gets the color Green.
        /// </summary>
        public static ColorRGBA Green
        {
            get
            {
                return new ColorRGBA(0, 255, 0, 255);
            }
        }

        /// <summary>
        /// Gets the color blue.
        /// </summary>
        public static ColorRGBA Blue
        {
            get
            {
                return new ColorRGBA(0, 0, 255, 255);
            }
        }

        /// <summary>
        /// Gets the color yellow.
        /// </summary>
        public static ColorRGBA Yellow
        {
            get
            {
                return new ColorRGBA(255, 255, 0, 255);
            }
        }

        #endregion

        #region Methods - Public static

        /// <summary>
        /// Attempt to parse a color from the passed string.
        /// </summary>
        /// <param name="value">String to parse a color from.</param>
        /// <param name="color">Destination color.</param>
        /// <returns>Returns true if the color was parsed successfully.</returns>
        public static bool TryParse(string value, out ColorRGBA color)
        {
            color = ColorRGBA.White;

            if (string.IsNullOrEmpty(value))
            {
                return false;
            }

            string[] parts = value.Split(' ');
            if (parts == null || parts.Length != 4)
            {
                return false;
            }

            bool success = true;

            success &= byte.TryParse(parts[0], out color.r);
            success &= byte.TryParse(parts[1], out color.g);
            success &= byte.TryParse(parts[2], out color.b);
            success &= byte.TryParse(parts[3], out color.a);

            return success;
        }

        #endregion

        #region Methods - Public

        /// <summary>
        /// Convert object to string.
        /// </summary>
        /// <returns>Returns a string representation of the object.</returns>
        public override string ToString()
        {
            return string.Format("{0} {1} {2} {3}", this.r, this.g, this.b, this.a);
        }

        #endregion
    }
}
